// pages/mycard/mycard.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    headSrc: '',
    avatarUrl: '',
    name: '暂未填写信息',
    tel: '',
    job: '暂未填写信息',
    company: '暂未填写信息',
    region: [],
    city: '',
    isUpdated: false
  },
  /**
   * 城市选择器
   */
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value,
      city: e.detail.value[0] + e.detail.value[1] + e.detail.value[2],
    })
  },
  /**
   * 上传头像
   */
  chooseImageTap: function (e) {
    var that = this;

    wx.chooseImage({
      success: res => {
        console.log(res);
        var tempFilePaths = res.tempFilePaths[0];
        console.log(tempFilePaths);
        var path = res.tempFilePaths;
        console.log(path);

        wx.saveFile({
          tempFilePath: path[0],
          success: function (res) {
            console.log(res);
            that.setData({
              headSrc: res.savedFilePath,
              isUpdated: true
            });
          }
        });
      }
    });
  },
  /**
   * 获取姓名
   */
  getName: function (e) {
    this.setData({
      name: e.detail.value
    })
  },
  /**
   * 获取职位
   */
  getJob: function (e) {
    console.log(e);
    console.log('职位:', e.detail.value)
    this.setData({
      job: e.detail.value
    })
  },
  /**
   * 获取组织机构
   */
  getCompany: function (e) {
    console.log(e);
    console.log('职位:', e.detail.value)
    this.setData({
      company: e.detail.value
    })
  },
  /**
   * 提交表单
   */
  formSubmit: function (e) {
    var that = this
    var reg = new RegExp("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$");
    if (e.detail.value.name.replace(/\s+/g, '') == "") {
      wx.showToast({
        title: '请输入姓名',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (!(/^1[34578]\d{9}$/.test(e.detail.value.tel))) {
      wx.showToast({
        title: '手机格式不正确',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (e.detail.value.tel == '') {
      wx.showToast({
        title: '手机格式不正确',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (e.detail.value.email == '') {
      wx.showToast({
        title: '请输入邮箱',
        icon: 'none',
        duration: 2000
      })
      return false
    } else if(!reg.test(e.detail.value.email)) {
      wx.showToast({
        title: '邮箱格式不正确',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    if (e.detail.value.company == '') {
      wx.showToast({
        title: '请填写组织/公司',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (e.detail.value.job == '') {
      wx.showToast({
        title: '请填写职位',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (this.data.isUpdated) {
      wx.uploadFile({
        url: app.globalData.url + 'Card/updCard',
        filePath: that.data.headSrc,
        name: "image",
        formData: {
          openid: wx.getStorageSync('openid'),
          image: that.data.headSrc,
          username: that.data.name,
          telephone: e.detail.value.tel,
          email: e.detail.value.email,
          company: e.detail.value.company,
          job: e.detail.value.job,
          business: e.detail.value.business,
          introduct: e.detail.value.introduct,
          city: that.data.city,
          address: e.detail.value.address,
        },

        success: (res) => {
          console.log(res);
          var res = JSON.parse(res.data);
          console.log(res);
          if (res.status == 1) {
            wx.showToast({
              title: res.data.msg,
              icon: 'success',
              duration: 2000
            })
            console.log(app.globalData.userInfo.avatarUrl);
            console.log(that.data.headSrc);
            
            app.globalData.userInfo.avatarUrl = that.data.headSrc
            console.log(app.globalData.userInfo.avatarUrl);
            setTimeout(function () {
              wx.navigateBack({

              });
            }, 2000)
          }

        }
      });
    } else {
      wx.request({
        url: app.globalData.url + 'Card/updCard',
        method: 'POST',
        data: {
          openid: wx.getStorageSync('openid'),
          image: that.data.headSrc,
          username: that.data.name,
          telephone: e.detail.value.tel,
          email: e.detail.value.email,
          company: e.detail.value.company,
          job: e.detail.value.job,
          business: e.detail.value.business,
          introduct: e.detail.value.introduct,
          city: that.data.city,
          address: e.detail.value.address,
        },
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success: function (res) {
          console.log(res);
          wx.showToast({
            title: res.data.msg,
            icon: 'success',
            duration: 2000
          })
          
          wx.navigateBack({
            delta: 1, // 回退前 delta(默认为1) 页面
          })
        }

      })
    }
  },
  // 去选择模板
  toChoose() {
    wx.navigateTo({
      url: '../chooseMould/chooseMould',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    var that = this
    wx.request({
      url: app.globalData.url + 'Card/index',
      method: 'GET',
      data: {
        openid: wx.getStorageSync('openid'),
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },    
      success: function (res) {
        console.log(res);
        that.setData({
          headSrc: res.data.data.image,
          name: res.data.data.username,
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})